package com.example.sneakerappforme1105.adapter;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.CheckBox;
import android.widget.CompoundButton;
import android.widget.ImageButton;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.recyclerview.widget.RecyclerView;

import com.bumptech.glide.Glide;
import com.example.sneakerappforme1105.GoodsDetailActivity;
import com.example.sneakerappforme1105.R;
import com.example.sneakerappforme1105.bean.CartItem;
import com.example.sneakerappforme1105.bean.Goods;
import com.example.sneakerappforme1105.util.ImageLoader;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;

public class CartItemAdapter extends RecyclerView.Adapter {
    private ArrayList<CartItem> mList;
    private Context mContext;
    private HashMap<Long, Goods> mGoodsMap = new HashMap<>();
    private CartItemAdapter.ItemButtonListenr mButtonListener;
    private OnItemLongClickListener onItemLongClickListener;
    // 定义一个循环视图列表项的长按监听器接口
    public interface OnItemLongClickListener {
        void onItemLongClick(View view, int position);
    }

    public interface ItemButtonListenr{
        public void onAddClick(View v, int position);
        public void onSubClick(View v, int position);
        public void onSetCheckBox(View v, int positon, boolean flag);
    }

    public void setButtonListener(CartItemAdapter.ItemButtonListenr mListener){
        this.mButtonListener = mListener;
    }

    public void setOnItemLongClickListener(OnItemLongClickListener mListener){
        this.onItemLongClickListener = mListener;
    }


    public CartItemAdapter(Context mContext, List<CartItem>mList, HashMap<Long,Goods>mGoodsMap){
        this.mList = (ArrayList<CartItem>) mList;
        this.mContext = mContext;
        this.mGoodsMap = mGoodsMap;
    }

    @Override
    public RecyclerView.ViewHolder onCreateViewHolder(ViewGroup viewGroup, int viewType) {
        // 根据单条数据的xml得到视图
        View v = LayoutInflater.from(mContext).inflate(R.layout.item_cart,viewGroup,false);
        return new ItemHolder(v);
    }

    @Override
    public void onBindViewHolder(RecyclerView.ViewHolder viewHolder, int position) {
        CartItem cartItem = mList.get(position);
        ItemHolder holder = (ItemHolder) viewHolder;
        holder.bindBean(cartItem);
        holder.bt_add.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                if(mButtonListener!=null){
                    mButtonListener.onAddClick(view, holder.getAdapterPosition());
                }
            }
        });

        holder.bt_sub.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                if(mButtonListener!=null){
                    mButtonListener.onSubClick(view, holder.getAdapterPosition());
                }
            }
        });

        holder.ck_choose.setChecked(cartItem.getIsChecked()==1);
        holder.ck_choose.setOnClickListener(view -> {
            boolean newFlag = holder.ck_choose.isChecked();
            holder.ck_choose.setChecked(newFlag);
            if(mButtonListener!=null){
                mButtonListener.onSetCheckBox(view, holder.getAdapterPosition(),newFlag);// 将选中的商品进行结算
            }
        });

        holder.ll_cart_item.setOnLongClickListener(view -> {
            if(onItemLongClickListener!=null){
                onItemLongClickListener.onItemLongClick(view,holder.getAdapterPosition());
            }

            return true;
        });


    }

    @Override
    public int getItemCount() {
        return mList.size();
    }

    public void updateData(List<CartItem> newCartItemList, HashMap<Long, Goods> newGoodsMap) {
        this.mList = (ArrayList<CartItem>) newCartItemList;
        this.mGoodsMap = newGoodsMap;
        notifyDataSetChanged();  // 通知 Adapter 数据已更新
    }


    private class ItemHolder extends RecyclerView.ViewHolder {
        private final TextView tv_name;
        private final TextView tv_desc;
        private final TextView tv_price;
        private ImageButton addBt;
        private ImageButton subBt;
        private final ImageView iv_thumb;
        private TextView tv_ItemNum;
        private ImageButton bt_add;
        private ImageButton bt_sub;
        private CheckBox ck_choose;
        private LinearLayout ll_cart_item;

        public ItemHolder(View itemView) {
            super(itemView);
            tv_name = itemView.findViewById(R.id.tv_name);
            tv_desc = itemView.findViewById(R.id.tv_desc);
            tv_price = itemView.findViewById(R.id.tv_price);
            iv_thumb = itemView.findViewById(R.id.iv_thumb);
            addBt = itemView.findViewById(R.id.button_add);
            subBt = itemView.findViewById(R.id.button_subtract);
            tv_ItemNum = itemView.findViewById(R.id.tv_ItemNum);
            bt_add = itemView.findViewById(R.id.button_add);
            bt_sub = itemView.findViewById(R.id.button_subtract);
            ck_choose = itemView.findViewById(R.id.ck_choose);
            ll_cart_item = itemView.findViewById(R.id.ll_cart_item);
        }

        void bindBean(CartItem item){
            Goods goods = mGoodsMap.get(item.getGoodsId()); // 当前购物车项目对应的商品
            tv_name.setText(goods.getName());
            tv_desc.setText(goods.getDesc() +" " +"尺码 "+item.getSize());
            tv_price.setText("￥"+String.valueOf(GoodsDetailActivity.formatDouble(item.getPrice()*item.getNum()) ));
//            iv_thumb.setImageResource(goods.getPicRes());
            Glide.get(mContext).clearMemory();
            ImageLoader.loadImage(mContext, goods.getPicPath(), iv_thumb);
            tv_ItemNum.setText(String.valueOf(item.getNum()));
        }
    }
}
